package com.sne.airways.system.dao.impl;

import com.sne.airways.basecore.dao.impl.BaseDAOHibernate;
import com.sne.airways.system.dao.UserDAO;
import com.sne.airways.system.pojo.TdUser;
import org.apache.commons.lang.StringUtils;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: skyline{http://my.oschina.net/skyline520}
 * Date: 12-11-23
 * Time: 下午5:47
 * To change this template use File | Settings | File Templates.
 */
@Repository
public class UserDAOHibernate extends BaseDAOHibernate implements UserDAO {
    /**
     * 查询用户列表
     *
     * @param userNo
     * @param userName
     * @param status
     * @param start
     * @param limit
     * @return
     */
    public List<TdUser> queryUserList(String userNo,String currentUserNo, String userName, String status, int start, int limit) {
        Session session = getSession();
        StringBuffer sb = new StringBuffer();
        sb.append(" select op from TdUser op where 1=1 ");
        if (StringUtils.isNotBlank(userNo)){
            sb.append(" and op.userNo = :userNo ");
        }
        if (StringUtils.isNotBlank(status)){
            sb.append(" and op.status = :status ");
        }
        if (StringUtils.isNotBlank(userName)){
            sb.append(" and op.userName like :userName ");
        }
        sb.append(" and  op.userNo !=:currentUserNo ");
        sb.append(" order by op.userNo ");
        Query query=session.createQuery(sb.toString());
        if (StringUtils.isNotBlank(userNo)){
            query.setString("userNo", userNo);
        }
        if (StringUtils.isNotBlank(status)){
            query.setString("status", status);
        }
        if (StringUtils.isNotBlank(userName)){
            query.setString("userName", "%" + userName + "%");
        }
        query.setString("currentUserNo", currentUserNo);
        return  query.setFirstResult(start).setMaxResults(limit).list();
    }

    /**
     * 查询满足条件的用户记录总数
     *
     * @param userNo
     * @param userName
     * @param status
     * @return
     */
    public Integer queryUserCount(String userNo,String currentUserNo, String userName, String status) {
        Session session = getSession();
        StringBuffer sb = new StringBuffer();
        sb.append(" select count(op) from TdUser op where 1=1 ");
        if (StringUtils.isNotBlank(userNo)){
            sb.append(" and op.userNo = :userNo ");
        }
        if (StringUtils.isNotBlank(status)){
            sb.append(" and op.status = :status ");
        }
        if (StringUtils.isNotBlank(userName)){
            sb.append(" and op.userName like :userName ");
        }
        sb.append(" and  op.userNo !=:currentUserNo ");
        Query query=session.createQuery(sb.toString());
        if (StringUtils.isNotBlank(userNo)){
            query.setString("userNo", userNo);
        }
        if (StringUtils.isNotBlank(status)){
            query.setString("status", status);
        }
        if (StringUtils.isNotBlank(userName)){
            query.setString("userName", "%" + userName + "%");
        }
        query.setString("currentUserNo", currentUserNo);
        return  ((Long)query.uniqueResult()).intValue();
    }
    /**
     * 查询用户
     *
     * @param userNo
     * @param status
     * @return
     */
    public Integer queryUserCount(String userId,String userNo, String status) {
        Session session = getSession();
        StringBuffer sb = new StringBuffer();
        sb.append(" select count(op) from TdUser op where 1=1 ");
        if (StringUtils.isNotBlank(userNo)){
            sb.append(" and op.userNo = :userNo ");
        }
        if (StringUtils.isNotBlank(status)){
            sb.append(" and op.status = :status ");
        }
        if (StringUtils.isNotBlank(userId)){
            sb.append(" and op.userId = :userId ");
        }
        Query query=session.createQuery(sb.toString());
        if (StringUtils.isNotBlank(userNo)){
            query.setString("userNo", userNo);
        }
        if (StringUtils.isNotBlank(status)){
            query.setString("status", status);
        }
        if (StringUtils.isNotBlank(userId)){
            query.setString("userId", userId);
        }
        return  ((Long)query.uniqueResult()).intValue();
    }

    /**
     * 查询用户列表
     *
     * @param userNo
     * @param userName
     * @param status
     * @return
     */
    @Override
    public List<TdUser> queryUserCheckedList(String userNo, String userName, String status) {
        Session session = getSession();
        StringBuffer sb = new StringBuffer();
        sb.append(" select op from TdUser op where 1=1 ");
        if (StringUtils.isNotBlank(userNo)){
            sb.append(" and op.userNo = :userNo ");
        }
        if (StringUtils.isNotBlank(status)){
            sb.append(" and op.status = :status ");
        }
        if (StringUtils.isNotBlank(userName)){
            sb.append(" and op.userName like :userName ");
        }
        sb.append(" order by op.userNo ");
        Query query=session.createQuery(sb.toString());
        if (StringUtils.isNotBlank(userNo)){
            query.setString("userNo", userNo);
        }
        if (StringUtils.isNotBlank(status)){
            query.setString("status", status);
        }
        if (StringUtils.isNotBlank(userName)){
            query.setString("userName", "%" + userName + "%");
        }
        return  query.list();
    }

    /**
     * 查询满足条件的用户记录总数
     *
     * @param userNo
     * @param userName
     * @param status
     * @return
     */
    @Override
    public Integer queryUserCheckedCount(String userNo, String userName, String status) {
        Session session = getSession();
        StringBuffer sb = new StringBuffer();
        sb.append(" select count(op) from TdUser op where 1=1 ");
        if (StringUtils.isNotBlank(userNo)){
            sb.append(" and op.userNo = :userNo ");
        }
        if (StringUtils.isNotBlank(status)){
            sb.append(" and op.status = :status ");
        }
        if (StringUtils.isNotBlank(userName)){
            sb.append(" and op.userName like :userName ");
        }
        Query query=session.createQuery(sb.toString());
        if (StringUtils.isNotBlank(userNo)){
            query.setString("userNo", userNo);
        }
        if (StringUtils.isNotBlank(status)){
            query.setString("status", status);
        }
        if (StringUtils.isNotBlank(userName)){
            query.setString("userName", "%" + userName + "%");
        }
        return  ((Long)query.uniqueResult()).intValue();
    }
}
